import jieba


with open('test.txt','rb') as f:
    text = f.read()


word_dict = {}
words = jieba.cut(text)
for word in words:
    if len(word) > 1:
        word_dict[word] = word_dict.get(word, 0) + 1


top_words = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)[:5]


with open('output.txt', 'w', encoding='utf-8') as f:
    f.write('出现次数前5的词汇和出现次数信息：\n')
    for word, freq in top_words:
        f.write(f'{word}: {freq}\n')



